package com.xcity.record;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

import com.xcity.merge.MergeUtil;

public class Mine extends BaseRecord {
	public static String SQL_PARA = "id, map, pos, mineType, ownerId, takeTime, extendCount, lastTakeTime, fightValue";

	public static String COUNT_SQL = "select count(id) from mine";
	public static String SELECT_LIMIT = "select * from mine limit {0},{1}";
	
	public static String RESET_MINE_TABLE = "TRUNCATE TABLE mine;\n";
	public static String RESET_MINE_CONFIG = "DELETE FROM config WHERE `key` LIKE \"%mapAvailablePoints%\";\nUPDATE config SET `value` = -1 WHERE `key` LIKE \"%mapIndex%\";\n";
	public static String RESET_PLAYER_MAP = "UPDATE player SET y = 0, `map` = -1;\n";
	
	protected int id;
	protected int map;
	protected int pos;
	protected byte mineType;
	protected long ownerId;
	protected long takeTime;
	protected byte extendCount;
	protected long lastTakeTime;
	protected long fightValue;
	
	@Override
	public void load(ResultSet rs) throws SQLException {
		this.id = rs.getInt(1);
		this.map = rs.getInt(2);
		this.pos = rs.getInt(3);
		this.mineType = rs.getByte(4);
		this.ownerId = rs.getLong(5);
		this.takeTime = rs.getLong(6);
		this.extendCount = rs.getByte(7);
		this.lastTakeTime = rs.getLong(8);
		this.fightValue = rs.getLong(9);
	}

	@Override
	public String getColumnNames() {
		return SQL_PARA;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append(MergeUtil.toSqlString(id));
		sb.append(", ");
		sb.append(MergeUtil.toSqlString(map));
		sb.append(", ");
		sb.append(MergeUtil.toSqlString(pos));
		sb.append(", ");
		sb.append(MergeUtil.toSqlString(mineType));
		sb.append(", ");
		sb.append(MergeUtil.toSqlString(ownerId));
		sb.append(", ");
		sb.append(MergeUtil.toSqlString(takeTime));
		sb.append(", ");
		sb.append(MergeUtil.toSqlString(extendCount));
		sb.append(", ");
		sb.append(MergeUtil.toSqlString(lastTakeTime));
		sb.append(", ");
		sb.append(MergeUtil.toSqlString(fightValue));
		return sb.toString();
	}

	@Deprecated
	public static List<Mine> getMineList(Connection conn, int start, int page) throws SQLException{
		List<Mine> ret = new ArrayList<Mine>();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			String sql = MessageFormat.format(SELECT_LIMIT, start + "", page + "");
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()){
				Mine mine = new Mine();
				mine.load(rs);
				ret.add(mine);
			}
		} finally {
			if(rs != null){
				rs.close();
			}
			if(ps != null){
				ps.close();
			}
		}
		return ret;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getMap() {
		return map;
	}

	public void setMap(int map) {
		this.map = map;
	}

	public int getPos() {
		return pos;
	}

	public void setPos(int pos) {
		this.pos = pos;
	}

	public byte getMineType() {
		return mineType;
	}

	public void setMineType(byte mineType) {
		this.mineType = mineType;
	}

	public long getOwnerId() {
		return ownerId;
	}

	public void setOwnerId(long ownerId) {
		this.ownerId = ownerId;
	}

	public long getTakeTime() {
		return takeTime;
	}

	public void setTakeTime(long takeTime) {
		this.takeTime = takeTime;
	}

	public byte getExtendCount() {
		return extendCount;
	}

	public void setExtendCount(byte extendCount) {
		this.extendCount = extendCount;
	}

	public long getLastTakeTime() {
		return lastTakeTime;
	}

	public void setLastTakeTime(long lastTakeTime) {
		this.lastTakeTime = lastTakeTime;
	}

	public long getFightValue() {
		return fightValue;
	}

	public void setFightValue(long fightValue) {
		this.fightValue = fightValue;
	}
	
}
